package com.lizhongbao.cloud.feign.feign;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Feign切面
 * @Author lizhongbao
 * @Date 2019/12/21 16:58
 * @Version 1.0
 **/
@Aspect
@Component
public class FeignAspect {

    /**
     * Pointcut 切入点
     * 匹配com.lizhongbao.cloud.*.web包下面的所有方法
     */
    @Pointcut("execution(public * com.lizhongbao.cloud.*.web.*.*(..))")
    public void executionService(){}

    /**
     * 方法执行前
     */
    @Before(value = "executionService()")
    public void doBefore(){
        /**
         * 将RequestAttributes对象设置为子线程共享
         * 解决异步调用无法传递header
         */
        ServletRequestAttributes attribute = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        RequestContextHolder.setRequestAttributes(attribute, true);
    }

}
